package com.java110.mall.shop.cmd.shopHouse;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.shop.ShopHouseDto;
import com.java110.intf.shop.IShopHouseInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 运营查询仓库
 */
@Java110Cmd(serviceCode = "shopHouse.queryAdminShopHouse")
public class QueryAdminShopHouseCmd extends Cmd {

    @Autowired
    private IShopHouseInnerServiceSMO shopHouseInnerServiceSMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        super.validatePageInfo(reqJson);
        super.validateAdmin(context);
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        ShopHouseDto shopHouseDto = BeanConvertUtil.covertBean(reqJson, ShopHouseDto.class);
        int count = shopHouseInnerServiceSMOImpl.queryShopHousesCount(shopHouseDto);

        List<ShopHouseDto> shopHouseDtos = null;
        if (count > 0) {
            shopHouseDtos = shopHouseInnerServiceSMOImpl.queryShopHouses(shopHouseDto);
            freshShopName(shopHouseDtos);
        } else {
            shopHouseDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) shopHouseDto.getRow()), count, shopHouseDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
        context.setResponseEntity(responseEntity);
    }

    private void freshShopName(List<ShopHouseDto> shopHouseDtos) {
        if (ListUtil.isNull(shopHouseDtos)) {
            return;
        }
        List<String> shopIds = new ArrayList<>();

        for (ShopHouseDto tmpShopHouseDto : shopHouseDtos) {
            shopIds.add(tmpShopHouseDto.getShopId());
        }

        ShopDto shopDto = new ShopDto();
        shopDto.setShopIds(shopIds.toArray(new String[shopIds.size()]));
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);
        if (ListUtil.isNull(shopDtos)) {
            return;
        }
        for (ShopHouseDto tmpShopHouseDto : shopHouseDtos) {
            for (ShopDto tmpShopDto : shopDtos) {
                if (!tmpShopDto.getShopId().equals(tmpShopHouseDto.getShopId())) {
                    continue;
                }
                tmpShopHouseDto.setShopName(tmpShopDto.getShopName());
            }
        }
    }
}
